package com.ruoyi.controller.system;

import java.util.List;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.enums.RuleType;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysControl;
import com.ruoyi.system.service.ISysControlService;
import com.ruoyi.system.vo.SysControlRes;
import com.ruoyi.system.vo.req.SysControlReq;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysChannel;
import com.ruoyi.system.service.ISysChannelService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 通道Controller
 *
 * @author Ericcc
 * @date 2021-02-12
 */
@Controller
@RequestMapping("/system/channel")
public class SysChannelController extends BaseController
{
    private String prefix = "system/channel";

    @Autowired
    private ISysChannelService sysChannelService;

    @Autowired
    private ISysControlService sysControlService;


    @RequiresPermissions("system:channel:list")
    @GetMapping()
    public String channel()
    {
        return prefix + "/channel";
    }

    /**
     * 查询通道列表
     */
    @RequiresPermissions("system:channel:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysChannel sysChannel)
    {
        startPage();
        List<SysChannel> list = sysChannelService.selectSysChannelList(sysChannel);
        return getDataTable(list);
    }

    /**
     * 导出通道列表
     */
    @RequiresPermissions("system:channel:export")
    @Log(title = "通道", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysChannel sysChannel)
    {
        List<SysChannel> list = sysChannelService.selectSysChannelList(sysChannel);
        ExcelUtil<SysChannel> util = new ExcelUtil<SysChannel>(SysChannel.class);
        return util.exportExcel(list, "channel");
    }

    /**
     * 新增通道
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存通道
     */
    @RequiresPermissions("system:channel:add")
    @Log(title = "通道", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysChannel sysChannel)
    {
        sysChannel.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(sysChannelService.insertSysChannel(sysChannel));
    }

    /**
     * 修改通道
     */
    @GetMapping("/edit/{channelId}")
    public String edit(@PathVariable("channelId") Long channelId, ModelMap mmap)
    {
        SysChannel sysChannel = sysChannelService.selectSysChannelById(channelId);
        mmap.put("sysChannel", sysChannel);
        return prefix + "/edit";
    }

    /**
     * 修改保存通道
     */
    @RequiresPermissions("system:channel:edit")
    @Log(title = "通道", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysChannel sysChannel)
    {
        sysChannel.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(sysChannelService.updateSysChannel(sysChannel));
    }

    /**
     * 删除通道
     */
    @RequiresPermissions("system:channel:remove")
    @Log(title = "通道", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysChannelService.deleteSysChannelByIds(ids));
    }


    /**
     * 查看通道风控
     */
    @GetMapping("/viewChannelControl/{channelId}")
    public String viewChannelControl(@PathVariable("channelId") Long channelId, ModelMap mmap)
    {
        SysControlRes sysControlRes = sysControlService.selectSysControlByChannel(channelId);
        sysControlRes.setRelationId(channelId);
        mmap.put("sysControl",sysControlRes);
        return prefix + "/channelControl";
    }

    /**
     * 编辑通道风控
     */
    @RequiresPermissions("system:channel:control")
    @Log(title = "编辑通道风控", businessType = BusinessType.UPDATE)
    @PostMapping("/editChannelControl")
    @ResponseBody
    public AjaxResult editChannelControl(@Validated SysControlReq sysControlReq)
    {
        SysControlRes sysControlRes = sysControlService.selectSysControlByChannel(sysControlReq.getRelationId());
        if (sysControlRes.getControlType()!=RuleType.CHANNEL.getCode()){
            sysControlReq.setControlId(null);
            SysControl sysControl = new SysControl();
            BeanUtils.copyProperties(sysControlReq,sysControl);
            sysControl.setStartTime(DateUtil.parse(sysControlReq.getStartTime()));
            sysControl.setEndTime(DateUtil.parse(sysControlReq.getEndTime()));
            sysControl.setControlType(RuleType.CHANNEL.getCode());
            return toAjax(sysControlService.insertSysControl(sysControl));
        }
        SysControl sysControl = new SysControl();
        BeanUtils.copyProperties(sysControlReq,sysControl);
        sysControl.setStartTime(DateUtil.parse(sysControlReq.getStartTime()));
        sysControl.setEndTime(DateUtil.parse(sysControlReq.getEndTime()));
        return toAjax(sysControlService.updateSysControl(sysControl));
    }
}
